package com.base.core.context.utils;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import com.gitee.magic.framework.head.exception.BusinessAdviceException;
import com.gitee.magic.framework.head.exception.BusinessException;

/**
 * 
 * @author mac
 *
 */
public class ExceptionWrapper {
	
	public static <R>R get(Supplier<R> func){
		try {
			return func.get();
		}catch(BusinessException|BusinessAdviceException e) {
			throw e;
		}catch(Exception e) {
			return null;
		}
	}
	
	public static <R>R get(Supplier<R> func,String message){
		try {
			return func.get();
		}catch(BusinessException|BusinessAdviceException e) {
			throw e;
		}catch(Exception e) {
			throw new BusinessAdviceException(e, message);
		}
	}
	
	public static <T>void accept(Consumer<T> func,T t){
		try {
			func.accept(t);
		}catch(BusinessException|BusinessAdviceException e) {
			throw e;
		}catch(Exception e) {
		}
	}
	
	public static <T>void accept(Consumer<T> func,T t,String message){
		try {
			func.accept(t);
		}catch(BusinessException|BusinessAdviceException e) {
			throw e;
		}catch(Exception e) {
			throw new BusinessAdviceException(e, message);
		}
	}
	
	public static <T, R>R apply(Function<T,R> func,T t){
		try {
			return func.apply(t);
		}catch(BusinessException|BusinessAdviceException e) {
			throw e;
		}catch(Exception e) {
			return null;
		}
	}
	
	public static <T, R>R apply(Function<T,R> func,T t,String message){
		try {
			return func.apply(t);
		}catch(BusinessException|BusinessAdviceException e) {
			throw e;
		}catch(Exception e) {
			throw new BusinessAdviceException(e, message);
		}
	}
	
}
